package com.gitee.l0km.codegen.base.plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.gitee.l0km.codegen.base.generator.GeneratorConfiguration;

/**
 * codegen插件抽象类
 * @author guyadong
 *
 */
public abstract class BasePlugin extends AbstractMojo {
	@Parameter(defaultValue = "${project}", readonly = true, required = true)
	protected MavenProject project;
	/**
	 * template root path
	 */
	@Parameter(defaultValue = GeneratorConfiguration.DEFAULT_ROOT )
	private String templateRoot="";
	/**
	 * Output file location
	 */
	@Parameter(defaultValue = "${project.build.directory}/generated-sources")
	private String outputDir;
	/**
	 * templates included folder path under template-root
	 */
	@Parameter(defaultValue = GeneratorConfiguration.DEFAULT_INCLUDE)
	private String include;
	/**
	 * Resource loader type: [class,file],default class
	 */
	@Parameter(defaultValue = GeneratorConfiguration.DEFAULT_LOADER)
	private String loader;
	public BasePlugin() {
	}
	
	/**
	 * template used folder path under template-root<br>
	 */
	protected abstract String getTemplateFolder();
	protected List<String> makeArgs(){
		ArrayList<String> args = new ArrayList<String>(5);
		args.add("--" + GeneratorConfiguration.TEMPLATE_ROOT_OPTION_LONG);
		args.add(this.templateRoot);
		args.add("--" + GeneratorConfiguration.TEMPLATE_FOLDER_OPTION_LONG);
		args.add(getTemplateFolder());
		args.add("--" + GeneratorConfiguration.TEMPLATE_INCLUDE_OPTION_LONG);
		args.add(this.include);
		args.add("--" + GeneratorConfiguration.OUTPUT_LOCATION_OPTION_LONG);
		args.add(this.outputDir);
		args.add("--" + GeneratorConfiguration.RESOURCE_LOADER_OPTION_LONG);
		args.add(this.loader);
		return args;
	}

	/**
	 * 返回当前项目的依赖库列表<br>
	 * 从{@link MavenProject#getArtifacts()}读取所有依赖库，
	 * 得到以路径分割符分割的依赖库列表字符串，包括当前项目的编译路径
	 */
	protected String buildClasspath() {
		String classpath = project.getArtifacts().stream()
				.map(artifact -> artifact.getFile().getAbsolutePath())
				.filter(file -> file != null)
				.collect(Collectors.joining(File.pathSeparator));
		/** 获取输出目录 */
		return classpath + File.pathSeparator + project.getBuild().getOutputDirectory();
	}
}
